package crawler.cnki.task;

import crawler.cnki.entity.Paper;
import crawler.cnki.entity.Patent;
import crawler.cnki.entity.Project;
import crawler.cnki.entity.Researcher;
import crawler.cnki.service.PaperService;
import crawler.cnki.service.PatentService;
import crawler.cnki.service.ProjectService;
import crawler.cnki.service.ResearcherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

@Component
public class DataPipeline implements Pipeline {
    @Autowired
    private PaperService paperService;
    @Autowired
    private ResearcherService researcherService;
    @Autowired
    private PatentService patentService;
    @Autowired
    private ProjectService projectService;

    @Override
    public void process(ResultItems resultItems, Task task) {
        //如果是需要保存的resultItem
        if(resultItems.get("itemType")!=null) {
            //Paper的保存
            if (resultItems.get("itemType").equals(1)) {
                Paper paper = new Paper(resultItems.get("title"), resultItems.get("author"),
                        resultItems.get("summary"), resultItems.get("url"),
                        resultItems.get("keywords"), resultItems.get("date"),
                        resultItems.get("journal"), resultItems.get("organization"), resultItems.get("DOI"));
                paperService.save(paper);
            } else if (resultItems.get("itemType").equals(4)) {
                Researcher researcher = new Researcher();
                researcher.setName(resultItems.get("name"));
                researcher.setOrganization(resultItems.get("organization"));
                researcherService.save(researcher);
            } else if (resultItems.get("itemType").equals(2)) {
                Patent patent = new Patent();
                patent.setTitle(resultItems.get("title"));
                patent.setSummary(resultItems.get("summary"));
                patent.setType(resultItems.get("type"));
                patent.setPatentID(resultItems.get("patentID"));
                patent.setApplyDate(resultItems.get("applyDate"));
                patent.setPublishID(resultItems.get("publishID"));
                patent.setPublicDate(resultItems.get("publicDate"));
                patent.setMainTypeNumber(resultItems.get("mainTypeNumber"));
                patent.setTypeNumber(resultItems.get("typeNumber"));
                patent.setOwner(resultItems.get("owner"));
                patent.setDesigner(resultItems.get("designer"));
                patent.setAddress(resultItems.get("address"));
                patent.setAgency(resultItems.get("agency"));
                patent.setAgent(resultItems.get("agent"));
                patent.setCode(resultItems.get("code"));
                patent.setDescription(resultItems.get("description"));
                patent.setStatus(resultItems.get("status"));
                patentService.save(patent);
            } else if (resultItems.get("itemType").equals(3)){
                Project project=new Project();
                project.setAuthor(resultItems.get("author"));
                project.setTitle(resultItems.get("title"));
                project.setSummary(resultItems.get("summary"));
                project.setCompany(resultItems.get("company"));
                project.setKeywords(resultItems.get("keywords"));
                project.setYear(resultItems.get("year"));
                project.setScope(resultItems.get("scope"));
                project.setPlan(resultItems.get("plan"));
                projectService.save(project);
            }
        }
    }
}
